﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using HospitalSystem.Core;
namespace HospitalSystem
{
    public partial class healthinfo : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["CID"] == null)
            {

                //MessageBoxurl("กรุณากดค้นหา หรือลงทะเบียนเด็กก่อน", "findchild.aspx");

            }
            else
            {




                //showsession();
                //_showimage.show_Images(cid_label, Image1);
                show_Images();
                    

                if (!IsPostBack)
                {

                    BindDatahealthinfo();
                    readtodatatable();
                    BindDatareg_healthshow();
                    check1st();


                }

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            insertdatatodbheal();
            Panel2.Visible = true;
            Panel1.Visible = false;
            Response.Redirect("healthinfo.aspx");


        }

        public void show_Images()
        {
            string sj = "";



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Child_Pic FROM [Person] where cid=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                sj = reader2["Child_Pic"].ToString();
            }
            else
            {
                sj = "~/pic/profile/nopic.jpg";
            }
            conn.Close();
            reader2.Close();


            Image1.ImageUrl = (sj);
        }



        protected void check1st()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
            //ถ้าว่างแสดงว่ามาครั้งแรก
            SqlCommand CmdSql2 = new SqlCommand("SELECT CID FROM [healthinfo_log] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();


            //มาครั้งแรกก็ให้ลงทะเบียน
            if (reader2.Read())
            {
                Panel2.Visible = true;
                Panel1.Visible = false;


            }
            else
            //เคยมาแล้วก็ไปดูข้อมูล
            {
                Panel2.Visible = false;
                Panel1.Visible = true;
            }
            conn.Close();
            reader2.Close();



        }















        public void BindDatahealthinfo()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            //string svccode1 = svccode;
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT code,dec FROM reg_heal", scon);
            //cmd.Parameters.AddWithValue("@svccode1", svccode);
            SqlDataReader reader = cmd.ExecuteReader();

            GridView1.DataSource = reader;
            GridView1.DataBind();
            scon.Close();

            readtodatatable();
        }








        public void readtodatatable()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtheal = new DataTable();

            SqlCommand cmd = new SqlCommand("SELECT code,Note from healthinfo where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader readerheal = cmd.ExecuteReader();

            dtheal.Columns.Add("code");
            dtheal.Columns.Add("Note");
            while (readerheal.Read() != false)
            {
                dtheal.Rows.Add(readerheal["code"].ToString(), (readerheal["Note"].ToString()));
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            readerheal.Close();


            for (int j = 0; j < dtheal.Rows.Count; j++)
            {
                string a = dtheal.Rows[j]["code"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CaseSelector4");
                    TextBox tb = (TextBox)GR.FindControl("TextBox3");
                    if (a == b)
                    {

                        tb.Text = dtheal.Rows[j]["Note"].ToString();
                        // tb.Text=
                        chk.Checked = true;
                    }
                }
            }


            scon.Close();




        }





        public void insertdatatodbheal()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            //bool selectorChecked4 = false;

            DataTable dt2heal = new DataTable();
            dt2heal.Columns.Add("SACTCode");

            dt2heal.Columns.Add("note");
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CaseSelector4");
                if (cb != null && cb.Checked)
                {
                    //selectorChecked4 = true;
                    TextBox rb = row.FindControl("TextBox3") as TextBox;
                    DataRow dr2heal;
                    dr2heal = dt2heal.NewRow();
                    string SACTCode = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    // string CID = GridView2.Rows[row.RowIndex].Cells[1].Text;
                    //SelectStat.Text += string.Format("{0} has been added <br />", SACTCode);
                    //  string CID = GridView1.Rows[row.RowIndex].Cells[1].Text;
                    string note = rb.Text;
                    dt2heal.Rows.Add(SACTCode, note);


                }
            }




            SqlCommand command2 = new SqlCommand("delete from [healthinfo] where  CID=@CID", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", Session["CID"]);
            command2.ExecuteNonQuery();
            scon.Close();



            for (int i = 0; i < dt2heal.Rows.Count; i++)
            {


                SqlCommand command = new SqlCommand("INSERT INTO [healthinfo] (code,CID,Note,[update]) VALUES('" + dt2heal.Rows[i]["SACTCode"].ToString() + "',@PSNumber,'" + dt2heal.Rows[i]["note"].ToString() + "',@update)", scon);
                scon.Open();
                command.Parameters.AddWithValue("@PSNumber", Session["CID"]);
                command.Parameters.AddWithValue("@update", DateTime.Now);
                command.ExecuteNonQuery();
                scon.Close();

                SqlCommand commandlog = new SqlCommand("INSERT INTO [healthinfo_log] (code,CID,Note,[update]) VALUES('" + dt2heal.Rows[i]["SACTCode"].ToString() + "',@PSNumber,'" + dt2heal.Rows[i]["note"].ToString() + "',@update)", scon);
                scon.Open();
                commandlog.Parameters.AddWithValue("@PSNumber", Session["CID"]);
                commandlog.Parameters.AddWithValue("@update", DateTime.Now);
                commandlog.ExecuteNonQuery();
                scon.Close();



            }




            BindDatahealthinfo();

        }

        protected void edit_Click(object sender, EventArgs e)
        {
            Panel2.Visible = false;
            Panel1.Visible = true;
        }










        // สร้างตารางโชว์ข้อมูล โชว์เป็น label
        public void BindDatareg_healthshow()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            //string svccode1 = svccode;
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT a.code as code,a.dec as dec FROM reg_heal a join healthinfo b on b.code=a.code where b.CID=@CID ", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);
            //cmd.Parameters.AddWithValue("@svccode1", svccode);
            SqlDataReader reader = cmd.ExecuteReader();

            GridView2.DataSource = reader;
            GridView2.DataBind();
            scon.Close();
            readtodatatable2();

        }





        public void readtodatatable2()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtedu2 = new DataTable();

            SqlCommand cmd = new SqlCommand("SELECT code,Note from healthinfo where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader8 = cmd.ExecuteReader();

            dtedu2.Columns.Add("code");
            dtedu2.Columns.Add("Note");
            while (reader8.Read() != false)
            {
                dtedu2.Rows.Add(reader8["code"].ToString(), (reader8["Note"].ToString()));
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            reader8.Close();


            for (int j = 0; j < dtedu2.Rows.Count; j++)
            {
                string a = dtedu2.Rows[j]["code"].ToString();
                foreach (GridViewRow GR in GridView2.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    //CheckBox chk = (CheckBox)GR.FindControl("CaseSelectoredu");
                    Label tb = (Label)GR.FindControl("Labeledu");
                    if (a == b)
                    {

                        tb.Text = dtedu2.Rows[j]["Note"].ToString();
                        // tb.Text=
                        //chk.Checked = true;
                    }
                }
            }


            scon.Close();







        }



    }
}